package com.lin.thread.task;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class InHbaseByPutTask implements Runnable{
    private static final ThreadLocal<List<Put>> putss = new ThreadLocal<>();

    private List<Put> puts;
    private Connection connection;


    public InHbaseByPutTask(List<Put> puts, Connection connection) {
        this.puts = puts;
        this.connection = connection;
    }

    public InHbaseByPutTask() {
    }

    @Override
    public void run() {
        try {
            Table table = connection.getTable(TableName.valueOf("MOMO_CHAT:MSG"));
            List<Put> puts = putss.get();
            if(puts == null || puts.size() == 0){
                table.put(this.puts);
                putss.set(this.puts);
            }else table.put(puts);

            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public Table getTable(){
//        Table table = tableT.get();
//        if (table == null){
//            try {
//                table = connection.getTable(TableName.valueOf("MOMO_CHAT:MSG"));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            tableT.set(table);
//        }
//
//        return table;
//    }
}
